//
//  SEGUsersListViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 14-8-20.
//  Copyright (c) 2014年 陈区. All rights reserved.
//

#import "SEGUsersListViewController.h"
#import "SEGFriendDetailViewController.h"

@interface SEGUsersListViewController ()

@end

@implementation SEGUsersListViewController

@synthesize listData;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    [super loadView];
    m_tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), CGRectGetHeight(self.view.bounds) - kNavigationBarAndStatusBarHeight) style:UITableViewStylePlain];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    m_tableView.backgroundView=nil;
    m_tableView.separatorStyle=UITableViewCellSelectionStyleNone;
    m_tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:m_tableView];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"意向用户";
    [self showCustomNavigationBackButton];
    
    // Do any additional setup after loading the view.
}


#pragma mark - tableview datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return  1;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.listData.count;
}

//每行的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.row == 0) {
        return 54;
    }
    return 44;
}
#pragma mark - tableview delegate
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdStr = @"CellReuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdStr];
    if(!cell)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdStr];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        float xPos = 20;
        float yPos;
        
        if (indexPath.row == 0) {
            CGRect cellBounds = CGRectMake(0, 10, cell.bounds.size.width, 0.5);
            UIImageView *linebg = [[UIImageView alloc] initWithFrame:cellBounds];
            linebg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:linebg];
            
            yPos = 15;
            
            CGRect cellBounds2 = CGRectMake(10, 53, cell.bounds.size.width, 0.5);
            UIImageView *linebg2 = [[UIImageView alloc] initWithFrame:cellBounds2];
            linebg2.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:linebg2];
            
        }else{
            yPos = 5;
            CGRect cellBounds;
            if (indexPath.row == ([self.listData count]-1)) {
                cellBounds = CGRectMake(0, 43, cell.bounds.size.width, 0.5);
            }else{
                cellBounds = CGRectMake(10, 43, cell.bounds.size.width, 0.5);
            }
            UIImageView *linebg = [[UIImageView alloc] initWithFrame:cellBounds];
            linebg.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:linebg];
        }
        
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPos, yPos, 32, 32)];
        imageView .layer.masksToBounds = YES;
        imageView.tag = 101;
        imageView .layer.cornerRadius = 5.f;
        imageView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")];
        [cell.contentView addSubview:imageView];

        
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPos+42, yPos+8,280, 20)];
        titleLabel.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
        titleLabel.backgroundColor = [UIColor clearColor];
        titleLabel.textColor = kColor_2;
        titleLabel.tag = 102;
        [cell.contentView addSubview:titleLabel];
    }
    ActivityMemberModel *model = [self.listData objectAtIndex:indexPath.row];
    
    UIImageView * imageView = (UIImageView *)[cell viewWithTag:101];
    
    if(model.userIcon)
    {
        NSString *iconUrl = [NSString stringWithFormat:@"%@%@",kClientImageRootURL,model.userIcon];
        [imageView setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")] options:SDWebImageLowPriority];
    }else
    {
        imageView.image=[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")];
    }

    UILabel * title = (UILabel *)[cell viewWithTag:102];
    title.text = model.nickName;

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SEGFriendDetailViewController *detailCtl = [[SEGFriendDetailViewController alloc] init];
    detailCtl.hidesBottomBarWhenPushed = YES;
//    [detailCtl addFriendEntityData:[self.listData objectAtIndex:indexPath.row]];
    detailCtl.friendId = [self.listData objectAtIndex:indexPath.row].userId;
    [self.navigationController pushViewController:detailCtl animated:YES];
}

//接受意向用户列表
-(void)reviceData:(NSArray *)arr
{
    self.listData = arr;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
